package cn.hh.user.controller;


import cn.hh.basic.execption.BusinessException;
import cn.hh.basic.util.AjaxResult;
import cn.hh.user.dto.LoginLo;
import cn.hh.user.dto.UserLo;
import cn.hh.user.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginInfoController {
    @Autowired
    private ILoginInfoService loginInfoService;

    /*登录添加信息*/
    @PostMapping
    public AjaxResult login(@RequestBody UserLo userLo){
        /*取出用户名和密码*/
        /*添加到登陆信息*/
        try {
            loginInfoService.register(userLo);
            return AjaxResult.me();
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("注册失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    /*登录操作*/
    @PostMapping("/login")
    public AjaxResult accuserId(@RequestBody LoginLo loginLo){
        try {
            Map<String,Object> map  = loginInfoService.login(loginLo);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("登陆失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    /*微信登录操作*/
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody Map<String,String> param){
        try {
            System.out.println("++++++微信登录控制层+++++");
            return  loginInfoService.wechatLogin(param.get("code"));
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("登陆失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    /*微信注册绑定操作*/
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> param){
        try {
            Map<String,Object> map = loginInfoService.wechatBinder(param);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("登陆失败" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
}
